<?php
/**
 * Booking Template for Room Booking component
 */ 

defined('_JEXEC') or die('Restricted access');  
$document =& JFactory::getDocument();
$minTime = explode(':',$this->minTime);
$maxTime = explode(':',$this->maxTime);
$js="
function disableRepeat(){
	var repeat_type = document.getElementById('repeat_type').value;
	if (repeat_type=='0'){
		document.getElementById('repeat_until').value='';
		document.getElementById('repeat_until').disabled=true;
	}
	else
		document.getElementById('repeat_until').disabled=false;
}

function clearRepeat(){
	if (document.getElementById('repeat_type').value=='0')
		document.getElementById('repeat_until').value='';
}

function check(value, control_minute){
	var where = (navigator.appName == 'Microsoft Internet Explorer') ? -1 : null;
	if(value==".$maxTime[0]."){
		control_minute.empty();
		for (var i = 0; i <= ".$maxTime[1]."; i+=5) {
			newElem = document.createElement('option');
			if(i<10) {
				newElem.text = '0'+i;
				newElem.value = '0'+i;
			}
			else{
				newElem.text = i;
				newElem.value = i;
			}
			control_minute.add(newElem, where);
		}
	}
	else  if(value==".$minTime[0]."){
		control_minute.empty();
		for (var i = parseInt('".$minTime[1]."'); i <= 55; i+=5) {
			newElem = document.createElement('option');
			if(i<10) {
				newElem.text = '0'+i;
				newElem.value = '0'+i;
			}
			else{
				newElem.text = i;
				newElem.value = i;
			}
			control_minute.add(newElem, where);
		}
	}
	else if(control_minute.options.length!=13) {
		control_minute.empty();
		for (var i = 0; i <=55; i+=5) {
			newElem = document.createElement('option');
			if(i<10) {
				newElem.text = '0'+i;
				newElem.value = '0'+i;
			}
			else{
				newElem.text = i;
				newElem.value = i;
			}
			control_minute.add(newElem, where);
		}
	}
}

	function submitbutton(pressbutton) {

	     var start_date_arr = document.getElementById('start_date').value.split('-');
	     var start_date = new Date(start_date_arr[0], start_date_arr[1]-1, start_date_arr[2], document.getElementById('start_hour').value, document.getElementById('start_minute').value );
         var end_date_arr = document.getElementById('end_date').value.split('-');
         var end_date = new Date(end_date_arr[0], end_date_arr[1]-1, end_date_arr[2], document.getElementById('end_hour').value, document.getElementById('end_minute').value );
	     var today = new Date();
         var form = document.adminForm;

                if (pressbutton == 'cancel') {
                        submitform( pressbutton );
                        return;
                }

                // do field validation
                if (form.short_description.value == ''){
                        alert('".JText::_( 'PLSHORTDESC', true )."');
                } else if (form.start_date.value == '' || form.start_hour.value=='' || form.start_minute.value==''){
                        alert('".JText::_( 'PLSTARTDATETIME', true )."');
				} else if (form.end_date.value =='' || form.end_hour.value=='' || form.end_minute.value==''){
                        alert('".JText::_( 'PLENDDATETIME', true )."');
				} else if (start_date>= end_date){
                        alert('".JText::_( 'LATERENDDATE', true )."'); \n";
if(!$this->we_opening) 
	$js.=		"} else if (start_date.getDay()==0 || start_date.getDay()==6 || end_date.getDay()==0 || end_date.getDay()==6 ) {
                  		alert( '".JText::_( 'NOSUNSAT', true )."' ); \n";	        	
$js.="           }  else if (form.id_room.value == '' ){
                        alert('".JText::_( 'PLSELROOM', true )."');
                } else {
                        submitform( pressbutton );
                }
        }";
$document->addScriptDeclaration($js);
?>
<form action="index.php" method="post" name="adminForm" >
<div class="col100">
	<fieldset class="adminform">
	<?php echo JText::_( 'FIELDSREQUIRED' ); ?>
		<legend><?php echo JText::_( 'BOOKING' ); ?></legend>
		<table class="admintable">
		<?php if ($this->item->id>0)
		echo '<tr>
			<td width="100" align="right" class="key">
				<label  for="id">
					'.JText::_( 'ID' ).':
				</label>
			</td>
			<td>
				<input class="text_area" type="text" name="id" id="id" size="10" disabled="disabled" value="'.$this->item->id.'" />
			</td>
		</tr>';?>
				<tr>
			<td width="100" align="right" class="key">
				<label for="short_description">
					<?php echo JText::_( 'SHORTDESC' ); ?>*:
				</label>
			</td>
			<td>
				<input class="inputbox " type="text" name="short_description" id="short_description" size="55" maxlength="250" style="width:300px" value="<?php if(isset($this->item->short_description)) echo $this->item->short_description;?>" />
			</td>
		</tr>
		<tr>
			<td width="100" align="right" class="key">
				<label for="description">
					<?php echo JText::_( 'DESC' ); ?>:
				</label>
			</td>
			<td>
				<textarea  class="inputbox " name="description" id="description" cols="50" rows="5" style="width:300px" ><?php if(isset($this->item->description)) echo $this->item->description;?></textarea>
			</td>
		</tr>
		<tr>
			<td width="20" align="right" class="key">
				<label for="start_date">
					<?php echo JText::_( 'STARTDATE' ); ?>*:
				</label>
			</td>
			<td>
				<?php
				$start_date=explode(' ',$this->item->start);
				$start_date=$start_date[0];
				echo JHTML::_('calendar', $start_date,'start_date','start_date','%Y-%m-%d','class="inputbox" size="10"'); ?>
			
				<label for="start_hour">&nbsp;&nbsp;&nbsp;&nbsp;
					<?php echo JText::_( 'HOUR' ); ?>*:
				</label>
			
			<?php
				$hours=array();
				$hour = new stdClass();
				$hour->value=null;
				$hour->name='--';
				array_push($hours, $hour);
				for($i=$minTime[0]; $i<=$maxTime[0]; $i++){
					$hour = new stdClass();
					if (strlen($i)<2) $i='0'.$i;
					$hour->value=$i;
					$hour->name=$i;
					array_push($hours, $hour);
				}
				$minutes=array();
				$minute= new stdClass();
				$minute->value=null;
				$minute->name='--';
				array_push($minutes, $minute);
				for($i=0; $i<56; $i+=5){
					$minute= new stdClass();
					if ($i<10) $i='0'.$i;
					$minute->value=$i;
					$minute->name=$i;
					array_push($minutes, $minute);
				}
				if(isset($this->item->start)){
					$selected_hour=explode(' ',$this->item->start);
					$selected_hour=explode(':',$selected_hour[1]);
					$selected_minutes=$selected_hour[1];
					$selected_hour=$selected_hour[0];
				}
				else{
					$selected_hour='--';
					$selected_minutes='--';
				}
				echo JHTML::_('select.genericlist',$hours, 'start_hour', 'class="inputbox" onchange=check(this.value,document.getElementById(\'start_minute\'))','value','name',$selected_hour);?>
				<label for="start_minute">
					:
				</label>
				<?php echo JHTML::_('select.genericlist',$minutes, 'start_minute', 'class="inputbox "','value','name',$selected_minutes);?>
			</td>
		</tr>
		<tr>
			<td width="20" align="right" class="key">
				<label for="end_date">
					<?php echo JText::_( 'ENDDATE' ); ?>*:
				</label>
			</td>
			<td>
				<?php
				
					$end_date=explode(' ',$this->item->end);
					$end_date=$end_date[0];
				if(isset($this->item->end)){
					$selected_hour=explode(' ',$this->item->end);
					$selected_hour=explode(':',$selected_hour[1]);
					$selected_minutes=$selected_hour[1];
					$selected_hour=$selected_hour[0];
				}
				else{
					$selected_hour='--';
					$selected_minutes='--';
				}
				echo JHTML::_('calendar', $end_date,'end_date','end_date','%Y-%m-%d','class="inputbox" size="10"'); ?>
		
				<label for="end_hour">&nbsp;&nbsp;&nbsp;&nbsp;
					<?php echo JText::_( 'HOUR' ); ?>*:
				</label>
			<?php	echo JHTML::_('select.genericlist',$hours, 'end_hour', 'class=" inputbox " onchange=check(this.value,document.getElementById(\'end_minute\'))','value','name',$selected_hour); ?>
				<label for="end_minute">
					:
				</label>
				<?php echo JHTML::_('select.genericlist',$minutes, 'end_minute', 'class=" inputbox"','value','name',$selected_minutes);?>
			</td>
		</tr>
		<tr>
			<td width="100" align="right" class="key">
				<label for="notes">
					<?php echo JText::_( 'NOTES' ); ?>:
				</label>
			</td>
			<td>
				<textarea  class="inputbox" name="notes" id="notes" cols="50" rows="5" style="width:300px"><?php if(isset($this->item->notes)) echo $this->item->notes;?></textarea>
			</td>
		</tr>
		
		<tr>
			<td width="20" align="right" class="key">
				<label for="id_room">
					<?php echo JText::_( 'ROOM' ); ?>*:
				</label>
			</td>
			<td>
				<?php echo $this->room; ?>
			</td>
		</tr>
		<?php if ($this->item->id==0)
		{echo '<tr>
			<td align="right" class="key">
				<label>
					'.JText::_( 'REPEAT' ).':
				</label for="repeat_type">
			</td>
			<td>
				<select name="repeat_type" id="repeat_type" onchange="javascript:disableRepeat();>
 					 <option   value="0"'; if ($this->item->repeat_type=="0") echo 'selected'; echo'>'.JText::_( 'NEVER' ).'</option>
  				     <option  value="day"'; if ($this->item->repeat_type=="day") echo 'selected'; echo'>'.JText::_( 'EVERYDAY' ).'</option>
  				 	 <option  value="week"'; if ($this->item->repeat_type=="week") echo 'selected'; echo'>'.JText::_( 'EVERYWEEK' ).'</option>
					 <option  value="2week"'; if ($this->item->repeat_type=="2week") echo 'selected'; echo'>'.JText::_( 'EVERY2WEEK' ).'</option>
					 <option onclick="javascript:document.getElementById(\'repeat_until\').disabled=false" value="month"'; if ($this->item->repeat_type=="month") echo 'selected'; echo'>'.JText::_( 'EVERYMONTH' ).'</option>
					 <option onclick="javascript:document.getElementById(\'repeat_until\').disabled=false" value="year"'; if ($this->item->repeat_type=="year") echo 'selected'; echo'>'.JText::_( 'EVERYYEAR' ).'</option>			</select>
		
			<td align="right" class="key">
				<label>'.JText::_( 'UNTIL' ).':
				</label for="repeat_until">
				
				'.JHTML::_('calendar', $this->item->repeat_until, 'repeat_until','repeat_until','%Y-%m-%d','class="inputbox validate-date" size="10" readonly="readonly" disabled="disabled" onchange="javascript:clearRepeat();"' ).'
			</td>
<script>
if (document.getElementById("repeat_type").value=="0")
{
	document.getElementById("repeat_until").disabled=true;
	document.getElementById("repeat_until").value="";
}
else
{
	document.getElementById("repeat_until").disabled=false;
}
</script>
		</tr>';
		}else{
			echo'
		<tr>
			<td width="20" align="right" class="key">
					'.JText::_( 'REPEAT' ).':
			</td>
			<td>';
				 
				switch($this->item->repeat_type){
				case "0":{
					echo JText::_( 'NEVER' );
					break;
				}
				case "day":{
					echo JText::_( 'EVERYDAY' );
				 	break;		
				}
				case "week":{
					echo JText::_( 'EVERYWEEK' );
				 	break;
				}
				case "2week":{
					echo JText::_( 'EVERY2WEEK' );
				 	break;
				}
				case "month":{
					echo JText::_( 'EVERYMONTH' );
				 	break;
				}
				case "year":{
					echo JText::_( 'EVERYYEAR' );
				 	break;
				}
			}
				echo'
			</td>
		</tr>
		<tr>
			<td width="20" align="right" class="key">
					'.JText::_( 'UNTIL' ).':
			</td>
			<td>';
				 if(isset($this->item->repeat_until)){
					$until = new DateTime($this->item->repeat_until);
					echo $until->format('Y-m-d');
				} else echo "-"; 
				echo'
			</td>
		</tr>';}
				?>
		<tr>
			<td width="20" align="right" class="key">
				<label for="user">
					<?php echo JText::_( 'USER' ); ?>*:
				</label>
			</td>
			<td>
				<?php
				if ($this->item->id_user==0)
					$this->item->id_user = JFactory::getUser()->id;
				echo JHTML::_('select.genericlist',$this->users, 'id_user', 'class=" inputbox"','id','name',$this->item->id_user);?>
			</td>
		</tr>
                <tr>
			<td width="100" align="right" class="key">
				<label  for="total">
					<?php echo JText::_( 'Total Cost' ); ?>:
				</label>
			</td>
			<td>
				<input class="text_area" type="text" name="id" id="id" size="20" disabled="disabled" value="<?php echo $this->item->total;?>" />
			</td>
		</tr>
	</table>
	</fieldset>
</div>
<div class="clr"></div>

<input type="hidden" name="option" value="com_roombooking" />
<input type="hidden" name="id" value="<?php echo $this->item->id; ?>" />
<input type="hidden" name="task" value="" />
<input type="hidden" name="controller" value="booking" />
</form>

